# Copyright 2009-present MongoDB, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

if(NOT BUILD_TESTING)
    set_property(DIRECTORY PROPERTY EXCLUDE_FROM_ALL TRUE)
endif()

if(BSONCXX_BUILD_SHARED)
    bsoncxx_add_library(bsoncxx_testing "bsoncxx-testing" SHARED)
else()
    bsoncxx_add_library(bsoncxx_testing "bsoncxx-testing" STATIC)
endif()

target_compile_definitions(bsoncxx_testing PUBLIC BSONCXX_TESTING)

add_executable(test_bson
    array.cpp
    bson_b_date.cpp
    bson_builder.cpp
    bson_get_values.cpp
    bson_serialization.cpp
    bson_types.cpp
    bson_util_itoa.cpp
    bson_validate.cpp
    bson_value.cpp
    exception_guard.cpp
    json.cpp
    oid.cpp
    optional.test.cpp
    view_or_value.cpp
    make_unique.test.cpp
    string_view.test.cpp
    type_traits.test.cpp
)

# Common target properties for test executables.
add_library(bsoncxx_test_properties INTERFACE)
target_link_libraries(bsoncxx_test_properties INTERFACE bsoncxx_testing ${libbson_target})
target_include_directories(bsoncxx_test_properties INTERFACE

    # Allow `#include <bson/bson.h>`
    ${libbson_include_directories}

    # Allow `#include <bsoncxx/test/...>` and `#include <third_party/...>`.
    ${CMAKE_CURRENT_SOURCE_DIR}/../..
)
target_compile_definitions(bsoncxx_test_properties INTERFACE ${libbson_definitions})
add_library(bsoncxx::test_properties ALIAS bsoncxx_test_properties)

# Avoid redundant recompilation of catch/main.cpp.
add_library(bsoncxx_test_properties_with_main STATIC EXCLUDE_FROM_ALL ${THIRD_PARTY_SOURCE_DIR}/catch/main.cpp)
target_link_libraries(bsoncxx_test_properties_with_main PUBLIC bsoncxx::test_properties)
add_library(bsoncxx::test_properties_with_main ALIAS bsoncxx_test_properties_with_main)

set_property(TARGET test_bson APPEND PROPERTY LINK_LIBRARIES bsoncxx::test_properties_with_main)

add_test(NAME bson COMMAND test_bson)

# Generate test to ensure macro guards behave properly.
if(ENABLE_MACRO_GUARD_TESTS)
    include(MacroGuardTest)

    add_macro_guard_test(
        PROJECT_NAME bsoncxx
        PROJECT_TEST_PROPERTIES_TARGET bsoncxx::test_properties
        GUARDED_MACROS
            BSONCXX_INLINE # compiler.hpp
            BSONCXX_CALL
            BSONCXX_INLINE_NAMESPACE_BEGIN # config.hpp (generated by CMake)
            BSONCXX_INLINE_NAMESPACE_END
            BSONCXX_POLY_USE_MNMLSTC
            BSONCXX_POLY_USE_SYSTEM_MNMLSTC
            BSONCXX_POLY_USE_BOOST
            BSONCXX_POLY_USE_IMPLS
            BSONCXX_POLY_USE_STD
            BSONCXX_VERSION_EXTRA # version.hpp (generated by CMake)
            BSONCXX_VERSION_MAJOR
            BSONCXX_VERSION_MINOR
            BSONCXX_VERSION_PATCH
            BSONCXX_API_H # export.hpp (generated by CMake)
            BSONCXX_API
            BSONCXX_PRIVATE
            BSONCXX_DEPRECATED
            BSONCXX_DEPRECATED_EXPORT
            BSONCXX_DEPRECATED_NO_EXPORT
            DEFINE_NO_DEPRECATED
            BSONCXX_NO_DEPRECATED
            BSONCXX_UNREACHABLE # prelude.hpp
            _bsoncxxDisableWarningImpl_for_GCC  # util.hpp
            _bsoncxxDisableWarningImpl_for_GNU
            _bsoncxxDisableWarningImpl_for_MSVC
            _bsoncxxDisableWarningImpl_for_Clang
            BSONCXX_PUSH_WARNINGS
            BSONCXX_POP_WARNINGS
            BSONCXX_DISABLE_WARNING
            BSONCXX_CONCAT
            BSONCXX_CONCAT_IMPL
            bsoncxx_cxx14_constexpr
            BSONCXX_FORCE_SEMICOLON
            BSONCXX_FWD
            BSONCXX_PRAGMA
            _bsoncxxPragma
            BSONCXX_STRINGIFY
            BSONCXX_STRINGIFY_IMPL
            BSONCXX_IF_MSVC
            BSONCXX_IF_GCC
            BSONCXX_IF_CLANG
            BSONCXX_IF_GNU_LIKE
            BSONCXX_RETURNS
        INCLUDE_PATTERNS
            "include/*.hpp" # Public headers.
            "lib/*.hh" # Private headers.
            "test/*.hh" # Test headers.
        EXCLUDE_REGEXES
            "(include|lib)/.*/(prelude|postlude)\.(hpp|hh)" # Macro guard headers.
            "(include|lib)/bsoncxx/v_noabi/bsoncxx/config/.*" # v_noabi config headers.
            "include/bsoncxx/v_noabi/bsoncxx/enums/.*" # v_noabi X-macro headers.
            ".*_symbols.hh" # Symbol headers.
    )
endif()

set_dist_list(src_bsoncxx_test_DIST
    CMakeLists.txt
    array.cpp
    bson_b_date.cpp
    bson_builder.cpp
    bson_get_values.cpp
    bson_serialization.cpp
    bson_types.cpp
    bson_util_itoa.cpp
    bson_validate.cpp
    bson_value.cpp
    catch.hh
    exception_guard.cpp
    exception_guard.hh
    json.cpp
    oid.cpp
    optional.test.cpp
    test_macro_guards.cpp.in
    to_string.hh
    view_or_value.cpp
    make_unique.test.cpp
    string_view.test.cpp
    type_traits.test.cpp
)
